<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父级id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级id"></el-input>
    </el-form-item>
    <el-form-item label="药品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="药品名称"></el-input>
    </el-form-item>
    <el-form-item label="药品编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="药品编码"></el-input>
    </el-form-item>
    <el-form-item label="药品编号" prop="number">
      <el-input v-model="dataForm.number" placeholder="药品编号"></el-input>
    </el-form-item>
    <el-form-item label="id全路径" prop="fullId">
      <el-input v-model="dataForm.fullId" placeholder="id全路径"></el-input>
    </el-form-item>
    <el-form-item label="name全路径" prop="fullName">
      <el-input v-model="dataForm.fullName" placeholder="name全路径"></el-input>
    </el-form-item>
    <el-form-item label="编号全路径" prop="fullNumber">
      <el-input v-model="dataForm.fullNumber" placeholder="编号全路径"></el-input>
    </el-form-item>
    <el-form-item label="药品类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="药品类型"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="创建用户id" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建用户id"></el-input>
    </el-form-item>
    <el-form-item label="修改用户id" prop="modifyUser">
      <el-input v-model="dataForm.modifyUser" placeholder="修改用户id"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除:1-正常,-1删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="逻辑删除:1-正常,-1删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getHisDrugCategoryObj, addHisDrugCategoryObj, putHisDrugCategoryObj} from '@/api/his/hisdrugcategory'
    import {mapGetters} from 'vuex'
    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          name: '',
          code: '',
          number: '',
          fullId: '',
          fullName: '',
          fullNumber: '',
          type: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
        },
        dataRule: {
          parentId: [
            { required: true, message: '父级id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '药品名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '药品编码不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '药品编号不能为空', trigger: 'blur' }
          ],
          fullId: [
            { required: true, message: 'id全路径不能为空', trigger: 'blur' }
          ],
          fullName: [
            { required: true, message: 'name全路径不能为空', trigger: 'blur' }
          ],
          fullNumber: [
            { required: true, message: '编号全路径不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '药品类型不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          ],
          modifyUser: [
            { required: true, message: '修改用户id不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '逻辑删除:1-正常,-1删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
        ...mapGetters(['permissions','userInfo'])
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getHisDrugCategoryObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                this.dataForm.modifyUser = this.userInfo.username
                putHisDrugCategoryObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                this.dataForm.createUser = this.userInfo.username
                this.dataForm.modifyUser = this.userInfo.username
                addHisDrugCategoryObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
